<?php
/*
 * 企业后台类
 * @arrange Mark
 * @package ixdcw
 * @file    company.class.php
 * @time    12-12-19 下午1:01
 */
defined('IN_IXDCW') or exit('Access Denied');
class CompanyAdmin extends BasicModule {
	private $_userid;
	private $_username;

	private static $_table;
	private static $_tableEdition;

	/**
	 * 文档存储的主表
	 * @return string
	 */
	private static function _getTable() {
		if (!empty(self::$_table)) {
			return self::$_table;
		} else {
			self::$_table = Table::name(Table::NAME_MERCHANT_DOC);
			return self::$_table;
		}
	}

	/**
	 * 版本存储的主表
	 * @return string
	 */
	private static function _getTableEdition() {
		if (!empty(self::$_tableEdition)) {
			return self::$_tableEdition;
		} else {
			self::$_tableEdition = Table::name(Table::NAME_MERCHANT_EDITION);
			return self::$_tableEdition;
		}
	}
	


	/**
	 * 获取列表
	 * @param        $condition
	 * @param string $order
	 * @param string $cache
	 * @return array
	 */
	function getList($condition, $order = 'did desc', $cache = '') {
		global $db, $items,$replacef,$replacet;
//		$num = $this->_db->count(self::_getTable(), $condition, $cache);
//		$items=$num;

//		$table = self::_getTable();
		$table  = Table::name(Table::NAME_MERCHANT_DOC);
		$tableCatid = Table::name(Table::NAME_MERCHANT_CATID);

		$sqlCount = "SELECT count(distinct(d.did)) as sum FROM $table d,$tableCatid c where $condition";
		$items = intval($db->getVar($sqlCount, 'sum'));
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($items);


		$sql  = "select d.did,d.eid,d.title,d.summary,d.company_thumb,d.company_size,d.company_money,d.company_year,d.level,d.authority  from $table d,$tableCatid c where ";
		$sql .= $condition;
		$sql .= " group by d.did ";
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;


		$members = $db->getAll($sql);

//		if($kw) {
//			$replacef = explode(' ', $kw);
//			$replacet = array_map('highlight', $replacef);
//		}
//		while ($r = $this->_db->fetchArray($result)) {
//			if($kw) $r['company'] = str_replace($replacef, $replacet, $r['company']);
//			$members[] = $r;
//		}
		return $members;
	}

	/**
	 * 获取版本
	 * @param        $condition
	 * @param string $order
	 * @return array
	 */
	function getEditionList($condition, $order = 'eid desc') {
		global $items,$kw,$replacef,$replacet;
		$num = $this->_db->count(self::_getTableEdition(), $condition);
		$items=$num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$table = self::_getTableEdition();
		$sql = "SELECT * FROM {$table}";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$members = array();

		$result  = $this->_db->query($sql);
		if($kw) {
			$replacef = explode(' ', $kw);
			$replacet = array_map('highlight', $replacef);
		}
		while ($r = $this->_db->fetchArray($result)) {
			if($kw) $r['company'] = str_replace($replacef, $replacet, $r['company']);
			$members[] = $r;
		}
		return $members;
	}


	public function getAuditList($condition='') {
		global $db;
		$table = self::_getTableEdition();
		$sql = "select * from $table where audit=0 $condition";
		$arr = $db->getAll($sql);
		return $arr;
	}




	/**
	 * 删除词条
	 * @param $eid
	 * @param $did
	 * @param $type
	 * @return bool
	 */
	public static function deleteDoc($did) {
		global $db;
		if(!$did) return false;
		if(is_array($did)) {
			$dids = implode(',', $did);
		} else {
			$dids = intval($did);
		}
		if($dids){
			$table = self::_getTable();
			$tableEdition = self::_getTableEdition();
			$tableCatid = Table::name(Table::NAME_MERCHANT_CATID);
			$db->query("delete from $table where did in($dids)");

			$db->query("delete from $tableEdition where did in($dids)");
			$db->query("delete from $tableCatid where did in($dids)");
			return true;
		}else{
			Prompt::message('删除失败！请检查该版本是否正在使用！','goback');
		}
		return false;
	}


	/**
	 * 删除版本
	 * @param $eid
	 * @param $did
	 * @return bool
	 */
	public function deleteEdition($eid,$did) {
		global $db;
		if(!$eid) return false;
		$table = self::_getTable();
		$tableEdition = self::_getTableEdition();
		$tableCatid = Table::name(Table::NAME_MERCHANT_CATID);

		$nowEid = $this->_db->getVar("select eid from $table where did={$did}");

		//假如删除多个版本
		if(is_array($eid)) {
			foreach($eid as $k=> $v) {
				//假如删除 正在使用的版本
				if($v == $nowEid) unset($eid[$k]);
			}
			$eids = implode(',', $eid);
		} else {
			if($eid == $nowEid) Prompt::message('删除失败！请检查该版本是否正在使用！','goback');
			$eids = intval($eid);
		}

		//执行删除 版本 类别
		if($eids){
			$db->query("delete from $tableEdition where eid in($eids)");
			$db->query("delete from $tableCatid where eid in($eids)");
			return true;
		}else{
			Prompt::message('删除失败！请检查该版本是否正在使用！','goback');
		}
		return false;
	}

	/**
	 * 设置显示版本
	 * @param $eid
	 * @param $did
	 */
	public function setLead($eid,$did) {
		global $db;
		$eids=implode($eid);
		$table = self::_getTable();
		$tableEdition = self::_getTableEdition();
		$edition = $this->_db->getOne("select * from $tableEdition where eid=$eids and audit=1");
		if($edition){
			extract($edition);  //分解数组

			$sql = Sql::update(Table::name(Table::NAME_MERCHANT_DOC), array(
				'eid'             => $eid,
				'summary'         => $summary,
				'company_thumb'   => $company_thumb,
				'company_type'    => $company_type,
				'company_size'    => $company_size,
				'company_year'    => $company_year,
				'company_unit'    => $company_unit,
				'company_money'   => $company_money,
				'company_areaid'  => $company_areaid,
				'company_address' => $company_address,
				'company_contact' => $company_contact,
				'company_telephone'=> $company_telephone,
				'company_mail'    => $company_mail,
				'company_business'=> $company_business,

				'reason'          => $reason,
				'authorid'        => $authorid,
				'author'          => $author,
				'time'            => $time,
			),'did='.$did);
			if (!$db->query($sql)) 	exit($db->error());

			//清空 类别 主要版本
			$sql = Sql::update(Table::name(Table::NAME_MERCHANT_CATID), array('authority' => 0 ), 'did='.$did);
			if (!$db->query($sql))  exit($db->error());

			//设置 类别 主要版本
			$sql = Sql::update(Table::name(Table::NAME_MERCHANT_CATID), array('authority' => 1 ), 'did='.$did.' and eid='.$eid);
			if (!$db->query($sql))  exit($db->error());
			return true;
		}
		return false;
	}

	/**
	 * 判断是否审核通过
	 * @param $eid
	 * @param $audit
	 * @return array|bool|mixed|mysqli_result|PDOStatement|string
	 */
	public function auditEdition($eid,$audit) {
		$table = self::_getTable();
		$tableEdition = self::_getTableEdition();
		if($eid && $audit){
			//审核通过
			if(is_array($eid)){
				$eids = implode(',', $eid);
			}else{
				$eids = $eid;
			}
			return $this->_db->query("update $tableEdition set audit=1 where eid in($eids)");
		}elseif($eid && !$audit){
			//取消审核
			if(is_array($eid)){
				$eids = implode(',', $eid);
			}else{
				$eids = $eid;
			}
			return $this->_db->query("update $tableEdition set audit=0 where eid in($eids)");
		}
		return false;
	}

	/**
	 * 启用/隐藏词条
	 * @param $did
	 * @param $authority
	 * @return array|bool|mixed|mysqli_result|PDOStatement|string
	 */
	public function authorityDoc($did,$authority) {
		if($did && $authority){
			//启用词条
			if(is_array($did)){
				$dids = implode(',', $did);
			}else{
				$dids = $did;
			}
			$table = Table::name(Table::NAME_COMPANY);
			foreach($did as $val){
				$arr = CompanyAdmin::getDocInfo($val);
				if($this->_db->getOne("select * from {$table} where company='{$arr['title']}'")){
					$this->_db->query("update $table set companyid=$val where company='{$arr['title']}'");
				}
			}

			$table = self::_getTable();
			return $this->_db->query("update $table set authority=1 where did in($dids)");

		}elseif($did && !$authority){
			//隐藏词条
			if(is_array($did)){
				$dids = implode(',', $did);
			}else{
				$dids = $did;
			}
			$table = self::_getTable();
			return $this->_db->query("update $table set authority=0 where did in($dids)");
		}
		return false;
	}

	/**
	 * 推荐/取消推荐词条
	 * @param $did
	 * @param $level
	 * @return array|bool|mixed|mysqli_result|PDOStatement|string
	 */
	public function levelDoc($did,$level) {
		$table = self::_getTable();
		if($did && $level){
			//设为推荐
			if(is_array($did)){
				$dids = implode(',', $did);
			}else{
				$dids = $did;
			}

			return $this->_db->query("update $table set level=1 where did in($dids)");
		}elseif($did && !$level){
			//取消推荐
			if(is_array($did)){
				$dids = implode(',', $did);
			}else{
				$dids = $did;
			}
			return $this->_db->query("update $table set level=0 where did in($dids)");
		}
		return false;
	}


	/**
	 * 对比版本
	 * @param $eid
	 * @return array
	 */
	public function compareEdition($eid) {
		$tableEdition = self::_getTableEdition();
		if(count($eid)>2 || count($eid)<2) Prompt::message('只可对比两个版本！','goback');
		if(is_array($eid)){
			foreach($eid as $val) {
				$edition[] = $this->_db->getOne("select * from $tableEdition where eid=$val");
			}
		}
		return $edition;
	}

	/**
	 * 获取词条信息
	 * @param $did
	 * @return array|null
	 */
	public  function getDocInfo($did) {

		$table = self::_getTable();
		return $this->_db->getOne("select * from $table where did=$did");
	}

	/**
	 * 获取词条下 版本的审核状态
	 * @param $did
	 * @return int
	 */
	public static function getEditionAudit($did) {
		global $db;
		$tableEdition = self::_getTableEdition();
		$sql = "select audit from $tableEdition where did=$did";
		$arr = $db->getAll($sql);
		foreach($arr as $val){
			foreach($val as $v){
				if(!$v) return 1;
			}
		}
		return 0;
	}

	/**
	 * 获取主要版本
	 * @param $did
	 * @return null
	 */
	public function getMainEdition($did) {
		global $db;
		$table = self::_getTable();
		$sql = "select eid from $table where did=$did";
		return $db->getVar($sql);
	}




}
